/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package frsf.cidisi.exercise.tpidemia.entidades;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;

/**
 *
 * @author Nacho
 */
public class Dijkstra {
    public static void computePaths(Vertex source)
    {
        source.minDistance = 0.;
        PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
      	vertexQueue.add(source);

	while (!vertexQueue.isEmpty()) {
	    Vertex u = vertexQueue.poll();

            // Visit each edge exiting u
            for (Edge e : u.adjacencies)
            {
                Vertex v = e.target;
                double weight = e.weight;
                double distanceThroughU = u.minDistance + weight;
		if (distanceThroughU < v.minDistance) {
		    vertexQueue.remove(v);
		    v.minDistance = distanceThroughU ;
		    v.previous = u;
		    vertexQueue.add(v);
		}
            }
        }
    }

    public static List<Vertex> getShortestPathTo(Vertex target)
    {
        List<Vertex> path = new ArrayList<Vertex>();
        for (Vertex vertex = target; vertex != null; vertex = vertex.previous)
            path.add(vertex);
        Collections.reverse(path);
        return path;
    }

    public double mejorCamino(String origen, String destino){
        
         
             
      Vertex ergintone = new Vertex("ergintone");  
      Vertex bulovoe = new Vertex("bulovoe");
      Vertex peregaey = new Vertex("peregaey");
      Vertex aragaey = new Vertex("aragaey");
      Vertex icaedur = new Vertex("icaedur");
      Vertex vinizaile = new Vertex("vinizaile");
      Vertex bresol = new Vertex("bresol");
      Vertex mixocu = new Vertex("mixocu");
      Vertex cabe = new Vertex("cabe");
      Vertex ase = new Vertex("ase");
      Vertex cenede = new Vertex("cenede");

	ergintone.adjacencies = new Edge[]{ new Edge(bulovoe, 200), new Edge(peregaey, 180), new Edge(aragaey, 100) };
	
        bulovoe.adjacencies = new Edge[]{ new Edge(ergintone, 200), new Edge(icaedur, 350) };
	
        peregaey.adjacencies = new Edge[]{ new Edge(ergintone, 180), new Edge(vinizaile, 450) };
        
        aragaey.adjacencies = new Edge[]{ new Edge(ergintone, 100), new Edge(bresol, 150)};
        
        icaedur.adjacencies = new Edge[]{ new Edge(bulovoe, 350), new Edge(vinizaile, 100)};
        
        vinizaile.adjacencies = new Edge[]{ new Edge(icaedur, 100), new Edge(peregaey, 450), new Edge(bresol, 400), new Edge(cabe, 200), new Edge(mixocu, 300) };
        
        bresol.adjacencies = new Edge[]{ new Edge(vinizaile, 400), new Edge(aragaey, 150)};
        
        cabe.adjacencies = new Edge[]{ new Edge(vinizaile, 200)};
        
        mixocu.adjacencies = new Edge[]{ new Edge(vinizaile, 300), new Edge(ase, 200)};
        
        ase.adjacencies = new Edge[]{ new Edge(mixocu, 200), new Edge(cenede, 200)};
        
        cenede.adjacencies = new Edge[]{ new Edge(ase, 200) };
        
        
        Vertex ori = null;
        if( origen.equals("ergintone")) ori = ergintone;
        if( origen.equals("bulovoe")) ori = bulovoe;
        if( origen.equals("peregaey")) ori = peregaey;
        if( origen.equals("aragaey")) ori = aragaey;
        if( origen.equals("icaedur")) ori = icaedur;
        if( origen.equals("vinizaile")) ori = vinizaile;
        if( origen.equals("bresol")) ori = bresol;
        if( origen.equals("mixocu")) ori = mixocu;
        if( origen.equals("cabe")) ori = cabe;
        if( origen.equals("ase")) ori = ase;
        if( origen.equals("cenede")) ori = cenede;
        
        Vertex desti = null;
        if( destino.equals("ergintone")) desti = ergintone;
        if( destino.equals("bulovoe")) desti = bulovoe;
        if( destino.equals("peregaey")) desti = peregaey;
        if( destino.equals("aragaey")) desti = aragaey;
        if( destino.equals("icaedur")) desti = icaedur;
        if( destino.equals("vinizaile")) desti = vinizaile;
        if( destino.equals("bresol")) desti = bresol;
        if( destino.equals("mixocu")) desti = mixocu;
        if( destino.equals("cabe")) desti = cabe;
        if( destino.equals("ase")) desti = ase;
        if( destino.equals("cenede")) desti = cenede;
        
        
	Vertex[] vertices = {desti};
        computePaths(ori);
	
        
        return desti.minDistance;
    }
}
